package com.hangwei.paperless.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hangwei.paperless.R;
import com.twelve.all.utils.NotFastClickListener;

public class HomeTableTitle {
    private Context mContext;
    private final LinearLayout mLl0;
    private final LinearLayout mLl1;
    private final LinearLayout mLl2;
    private final LinearLayout mLl3;
    private final LinearLayout mLl4;
    private final LinearLayout mLl5;
    private final LinearLayout mLl6;
    private final LinearLayout mLl7;
    private final ImageView mUp0;
    private final ImageView mUp1;
    private final ImageView mUp2;
    private final ImageView mUp3;
    private final ImageView mUp4;
    private final ImageView mUp5;
    private final ImageView mUp6;
    private final ImageView mUp7;
    private final ImageView mDown0;
    private final ImageView mDown1;
    private final ImageView mDown2;
    private final ImageView mDown3;
    private final ImageView mDown4;
    private final ImageView mDown5;
    private final ImageView mDown6;
    private final ImageView mDown7;

    public View getInflate() {
        return mInflate;
    }

    public interface OrderByListener {
        void onOrderBy(String pageOrderName, String pageOrderBy);
    }

    public void setL(OrderByListener l) {
        this.l = l;
    }

    private OrderByListener l;

    private final View mInflate;

//    private static final String[] titles = {"任务号","名称","件号","序列号","卡片编号","状态","优先级","进度"};

    public HomeTableTitle(Context context, ViewGroup parent) {
        mContext = context;
        mInflate = LayoutInflater.from(context).inflate(R.layout.table_title_home, parent, false);
        mLl0 = mInflate.findViewById(R.id.ll0);
        mLl1 = mInflate.findViewById(R.id.ll1);
        mLl2 = mInflate.findViewById(R.id.ll2);
        mLl3 = mInflate.findViewById(R.id.ll3);
        mLl4 = mInflate.findViewById(R.id.ll4);
        mLl5 = mInflate.findViewById(R.id.ll5);
        mLl6 = mInflate.findViewById(R.id.ll6);
        mLl7 = mInflate.findViewById(R.id.ll7);
        mUp0 = mInflate.findViewById(R.id.up0);
        mUp1 = mInflate.findViewById(R.id.up1);
        mUp2 = mInflate.findViewById(R.id.up2);
        mUp3 = mInflate.findViewById(R.id.up3);
        mUp4 = mInflate.findViewById(R.id.up4);
        mUp5 = mInflate.findViewById(R.id.up5);
        mUp6 = mInflate.findViewById(R.id.up6);
        mUp7 = mInflate.findViewById(R.id.up7);
        mDown0 = mInflate.findViewById(R.id.down0);
        mDown1 = mInflate.findViewById(R.id.down1);
        mDown2 = mInflate.findViewById(R.id.down2);
        mDown3 = mInflate.findViewById(R.id.down3);
        mDown4 = mInflate.findViewById(R.id.down4);
        mDown5 = mInflate.findViewById(R.id.down5);
        mDown6 = mInflate.findViewById(R.id.down6);
        mDown7 = mInflate.findViewById(R.id.down7);
        mLl0.setTag(0);
        mLl1.setTag(0);
        mLl2.setTag(0);
        mLl3.setTag(0);
        mLl4.setTag(0);
        mLl5.setTag(0);
        mLl6.setTag(0);
        setOnClick(mLl0, mUp0, mDown0, "woNo");
        setOnClick(mLl1, mUp1, mDown1, "description");
        setOnClick(mLl2, mUp2, mDown2, "pn");
        setOnClick(mLl3, mUp3, mDown3, "sn");
        setOnClick(mLl4, mUp4, mDown4, "number");
        setOnClick(mLl5, mUp5, mDown5, "state");
        setOnClick(mLl6, mUp6, mDown6, "priority");
//        setOnClick(ll7, up7, down7, "progress");
    }

    private void setOnClick(LinearLayout ll, ImageView up, ImageView down, String pageOrderName) {
        ll.setOnClickListener(new NotFastClickListener() {
            @Override
            protected void isNotFastClick(View v) {
                setNone(ll);
                int i = (int) ll.getTag();
                switch (i % 3) {
                    case 0:
                        up.setVisibility(View.INVISIBLE);
                        down.setVisibility(View.VISIBLE);
                        if (l != null)
                            l.onOrderBy(pageOrderName, "asc");
                        break;
                    case 1:
                        up.setVisibility(View.VISIBLE);
                        down.setVisibility(View.INVISIBLE);
                        if (l != null)
                            l.onOrderBy(pageOrderName, "desc");
                        break;
                    case 2:
                        up.setVisibility(View.VISIBLE);
                        down.setVisibility(View.VISIBLE);
                        if (l != null)
                            l.onOrderBy("", "");
                        break;
                }
                ll.setTag(i + 1);
            }
        });
    }

    private void setNone(LinearLayout ll) {
        mUp0.setVisibility(View.VISIBLE);
        mUp1.setVisibility(View.VISIBLE);
        mUp2.setVisibility(View.VISIBLE);
        mUp3.setVisibility(View.VISIBLE);
        mUp4.setVisibility(View.VISIBLE);
        mUp5.setVisibility(View.VISIBLE);
        mUp6.setVisibility(View.VISIBLE);
//        mUp7.setVisibility(View.VISIBLE);
        mDown0.setVisibility(View.VISIBLE);
        mDown1.setVisibility(View.VISIBLE);
        mDown2.setVisibility(View.VISIBLE);
        mDown3.setVisibility(View.VISIBLE);
        mDown4.setVisibility(View.VISIBLE);
        mDown5.setVisibility(View.VISIBLE);
        mDown6.setVisibility(View.VISIBLE);
//        mDown7.setVisibility(View.VISIBLE);
        setNoneLl(ll);
    }

    private void setNoneLl(LinearLayout ll) {
        if (mLl0 != ll)
            mLl0.setTag(0);
        if (mLl1 != ll)
            mLl1.setTag(0);
        if (mLl2 != ll)
            mLl2.setTag(0);
        if (mLl3 != ll)
            mLl3.setTag(0);
        if (mLl4 != ll)
            mLl4.setTag(0);
        if (mLl5 != ll)
            mLl5.setTag(0);
        if (mLl6 != ll)
            mLl6.setTag(0);
        if (mLl7 != ll)
            mLl7.setTag(0);
    }
}
